<template>
  <div class="photo-view-page">
    <!-- 顶部导航栏 -->
    <header class="page-header">
      <div class="header-content">
        <h1 class="page-title">
          <el-icon>
            <Picture />
          </el-icon>
          {{ uavName }} - 无人机回传照片
        </h1>
        <div class="header-actions">
          <el-button
            type="primary"
            class="back-home-btn"
            @click="handleBackHome"
          >
            <el-icon>
              <HomeFilled />
            </el-icon>
            返回首页
          </el-button>
          <el-button link class="refresh-btn" @click="refreshAllPhotos">
            <el-icon>
              <Refresh />
            </el-icon>
            刷新全部
          </el-button>
          <el-button link class="fullscreen-btn" @click="toggleFullscreen">
            <el-icon>
              <FullScreen />
            </el-icon>
            全屏显示
          </el-button>
        </div>
      </div>
    </header>

    <!-- 照片查看区域 -->
    <main class="photo-container">
      <!-- 布局切换器 -->
      <div class="layout-switcher"></div>

      <!-- 照片网格布局 -->
      <div class="photo-grid" :class="gridClass">
        <div
          v-for="(photo, index) in visiblePhotos"
          :key="index"
          class="photo-card"
        >
          <div class="photo-header">
          </div>

          <div class="photo-content">
            <div class="photo-wrapper">
              <el-image
                :src="photo.url"
                :preview-src-list="[photo.url]"
                fit="cover"
                class="photo-display"
                :alt="photo.title"
              >
                <template #error>
                  <div class="image-error">
                    <el-icon>
                      <Picture />
                    </el-icon>
                    <span>加载失败</span>
                  </div>
                </template>
              </el-image>

              <!-- 照片控制覆盖层 -->
              <div class="photo-controls-overlay">
                <el-button
                  link
                  class="control-btn"
                  @click="handlePhotoOperate(photo.id, 'download')"
                >
                  <el-icon>
                    <Download />
                  </el-icon>
                </el-button>
                <el-button
                  link
                  class="control-btn"
                  @click="handlePhotoOperate(photo.id, 'fullscreen')"
                >
                  <el-icon>
                    <FullScreen />
                  </el-icon>
                </el-button>
              </div>
            </div>

            <!-- 照片信息栏 -->
            <div class="photo-info">
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
const route = useRoute();

// 获取路由参数
const uavId = ref(route.query.uavId || "");
const uavName = ref(route.query.uavName || "无人机");

// 照片数据
const photoList = ref([
  {
    id: 1,
    title: "巡检区域A - 主线路检查",
    url: require("@/assets/uavPhotoes/1.png"),
    updateTime: "2025-07-20 15:30:45",
    resolution: "1920x1080",
    fileSize: "2.4 MB",
  },
  {
    id: 2,
    title: "巡检区域B - 设备状态检查",
    url: require("@/assets/uavPhotoes/2.png"),
    updateTime: "2025-07-20 15:28:12",
    resolution: "1920x1080",
    fileSize: "1.8 MB",
  },
  {
    id: 3,
    title: "巡检区域C - 安全排查",
    url: require("@/assets/uavPhotoes/3.png"),
    updateTime: "2025-07-20 15:29:36",
    resolution: "1280x720",
    fileSize: "1.2 MB",
  },
]);

// 布局控制
const currentLayout = ref("6");
const visiblePhotos = computed(() => {
  if (currentLayout.value === "1") {
    return [photoList.value[0]];
  } else if (currentLayout.value === "4") {
    return photoList.value.slice(0, 4);
  } else {
    return photoList.value;
  }
});

const gridClass = computed(() => {
  if (currentLayout.value === "1") {
    return "grid-layout-1";
  } else if (currentLayout.value === "4") {
    return "grid-layout-4";
  } else {
    return "grid-layout-6";
  }
});

// 返回首页
const handleBackHome = () => {
  router.push("/picture-manage");
};

// 刷新所有照片
const refreshAllPhotos = () => {
  photoList.value.forEach((photo) => {
    photo.updateTime = new Date().toLocaleTimeString();
  });
  ElMessage({
    message: "所有照片已刷新",
    type: "success",
    duration: 1500,
  });
};

// 切换全屏
const toggleFullscreen = () => {
  const docEl = document.documentElement;

  if (!document.fullscreenElement) {
    if (docEl.requestFullscreen) {
      docEl.requestFullscreen();
    } else if (docEl.mozRequestFullScreen) {
      // Firefox
      docEl.mozRequestFullScreen();
    } else if (docEl.webkitRequestFullscreen) {
      // Chrome, Safari
      docEl.webkitRequestFullscreen();
    } else if (docEl.msRequestFullscreen) {
      // IE/Edge
      docEl.msRequestFullscreen();
    }
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  }
};

// 照片操作
const handlePhotoOperate = (photoId, type) => {
  if (type === "download") {
    // 实现真正的下载功能
    const photo = photoList.value.find((p) => p.id === photoId);
    if (photo) {
      // 创建一个临时的a标签来下载图片
      const link = document.createElement("a");
      link.href = photo.url;
      link.download = `${photo.title}_${new Date().getTime()}.jpg`;
      link.target = "_blank";

      // 添加到DOM中并触发点击
      document.body.appendChild(link);
      link.click();

      // 清理DOM
      document.body.removeChild(link);

      ElMessage({
        message: `正在下载照片: ${photo.title}`,
        type: "success",
        duration: 1500,
      });
    }
  } else if (type === "fullscreen") {
    // 实现真正的全屏查看功能
    const photo = photoList.value.find((p) => p.id === photoId);
    if (photo) {
      // 创建全屏查看的模态框
      const modal = document.createElement("div");
      modal.style.cssText = `
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background-color: rgba(0, 0, 0, 0.9);
        z-index: 9999;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
      `;

      // 创建图片元素
      const img = document.createElement("img");
      img.src = photo.url;
      img.style.cssText = `
        max-width: 90vw;
        max-height: 90vh;
        object-fit: contain;
        border-radius: 8px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
      `;
      img.alt = photo.title;

      // 创建关闭按钮
      const closeBtn = document.createElement("div");
      closeBtn.innerHTML = "×";
      closeBtn.style.cssText = `
        position: absolute;
        top: 20px;
        right: 30px;
        color: white;
        font-size: 40px;
        cursor: pointer;
        width: 50px;
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(0, 0, 0, 0.5);
        border-radius: 50%;
        transition: background-color 0.3s;
      `;

      // 创建标题
      const title = document.createElement("div");
      title.textContent = photo.title;
      title.style.cssText = `
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translateX(-50%);
        color: white;
        font-size: 18px;
        background-color: rgba(0, 0, 0, 0.7);
        padding: 10px 20px;
        border-radius: 20px;
        max-width: 80%;
        text-align: center;
      `;

      // 添加事件监听器
      const closeModal = () => {
        document.body.removeChild(modal);
        document.body.style.overflow = "auto";
      };

      modal.addEventListener("click", closeModal);
      closeBtn.addEventListener("click", (e) => {
        e.stopPropagation();
        closeModal();
      });
      img.addEventListener("click", (e) => {
        e.stopPropagation();
      });

      // 添加键盘ESC键关闭
      const handleEsc = (e) => {
        if (e.key === "Escape") {
          closeModal();
          document.removeEventListener("keydown", handleEsc);
        }
      };
      document.addEventListener("keydown", handleEsc);

      // 组装模态框
      modal.appendChild(img);
      modal.appendChild(closeBtn);
      modal.appendChild(title);

      // 添加到页面并禁用滚动
      document.body.appendChild(modal);
      document.body.style.overflow = "hidden";
    }
  }
};

// 组件挂载时初始化
onMounted(() => {
  // 这里可以根据uavId加载对应的照片数据
  console.log("加载无人机照片，ID:", uavId.value);
});
</script>

<style scoped>
.photo-view-page {
  width: 100%;
  min-height: 100vh;
  background-color: #0f172a;
  color: #e2e8f0;
  overflow-x: hidden;
}

/* 页面头部样式 */
.page-header {
  background-color: #1e293b;
  padding: 0 clamp(1rem, 2vw, 1.5rem);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.header-content {
  max-width: 1920px;
  margin: 0 auto;
  height: clamp(3.5rem, 5vw, 4.5rem);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  color: #ffffff;
  font-size: clamp(1.25rem, 2vw, 1.5rem);
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
}

.page-title .el-icon {
  margin-right: clamp(0.5rem, 1vw, 0.75rem);
  font-size: clamp(1.25rem, 2vw, 1.5rem);
  color: #3b82f6;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: clamp(0.5rem, 1vw, 0.75rem);
}

.back-home-btn {
  background-color: #3b82f6;
  color: #ffffff;
  border: none;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  padding: clamp(0.375rem, 0.75vw, 0.5rem) clamp(0.75rem, 1.5vw, 1rem);
}

.back-home-btn:hover {
  background-color: #2563eb;
}

.refresh-btn,
.fullscreen-btn {
  color: #cbd5e1;
  transition: all 0.3s;
  padding: clamp(0.375rem, 0.75vw, 0.5rem);
}

.refresh-btn:hover,
.fullscreen-btn:hover {
  color: #ffffff;
  background-color: rgba(255, 255, 255, 0.1);
}

/* 布局切换器 */
.layout-switcher {
  max-width: 1920px;
  margin: clamp(1.5rem, 2vw, 2rem) auto clamp(1rem, 1.5vw, 1.25rem);
  padding: 0 clamp(1rem, 2vw, 1.5rem);
  display: flex;
  justify-content: flex-end;
}

.layout-switcher .el-radio-button__inner {
  background-color: #1e293b;
  color: #94a3b8;
  border-color: #334155;
  padding: clamp(0.375rem, 0.75vw, 0.5rem) clamp(0.75rem, 1.5vw, 1rem);
  font-size: clamp(0.875rem, 1vw, 1rem);
}

.layout-switcher
  .el-radio-button__orig-radio:checked
  + .el-radio-button__inner {
  background-color: #3b82f6;
  color: #ffffff;
  border-color: #3b82f6;
  box-shadow: -1px 0 0 0 #3b82f6;
}

/* 照片网格布局 */
.photo-container {
  max-width: 1920px;
  margin: 0 auto;
  padding: 0 clamp(1rem, 2vw, 1.5rem) clamp(3rem, 4vw, 4rem);
}

.photo-grid {
  display: grid;
  gap: clamp(1rem, 1.5vw, 1.25rem);
}

.grid-layout-1 {
  grid-template-columns: 1fr;
}

.grid-layout-4 {
  grid-template-columns: repeat(2, 1fr);
}

.grid-layout-6 {
  grid-template-columns: repeat(3, 1fr);
}

/* 照片卡片 */
.photo-card {
  background-color: #1e293b;
  border-radius: clamp(0.5rem, 1vw, 0.75rem);
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s, box-shadow 0.3s;
  border: 1px solid #334155;
}

.photo-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

/* 照片头部 */
.photo-header {
  padding: clamp(0.75rem, 1vw, 1rem) clamp(1rem, 1.5vw, 1.25rem);
  background-color: #1e293b;
  border-bottom: 1px solid #334155;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
}

.photo-title {
  font-size: clamp(1rem, 1.25vw, 1.125rem);
  font-weight: 500;
  color: #ffffff;
  display: flex;
  align-items: center;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.camera-icon {
  margin-right: clamp(0.5rem, 0.75vw, 0.625rem);
  color: #3b82f6;
}

.photo-status {
  display: flex;
  gap: clamp(1rem, 1.5vw, 1.25rem);
  font-size: clamp(0.875rem, 1vw, 1rem);
  white-space: nowrap;
}

.status-item {
  display: inline-flex;
  align-items: center;
  color: #94a3b8;
}

.status-dot {
  width: clamp(0.5rem, 0.75vw, 0.625rem);
  height: clamp(0.5rem, 0.75vw, 0.625rem);
  border-radius: 50%;
  margin-right: clamp(0.375rem, 0.5vw, 0.5rem);
  flex-shrink: 0;
}

.online {
  background-color: #00b42a;
}

/* 照片内容区域 */
.photo-content {
  position: relative;
  width: 100%;
  height: 100%;
}

.photo-wrapper {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 75%;
  /* 4:3 比例 */
}

.photo-display {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-color: #000000;
}

.image-error {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #374151;
  color: #9ca3af;
  font-size: clamp(0.875rem, 1vw, 1rem);
}

.image-error .el-icon {
  font-size: clamp(2rem, 3vw, 2.5rem);
  margin-bottom: clamp(0.5rem, 0.75vw, 0.625rem);
}

/* 照片控制覆盖层 */
.photo-controls-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    to top,
    rgba(0, 0, 0, 0.7) 0%,
    rgba(0, 0, 0, 0) 60%
  );
  opacity: 0;
  transition: opacity 0.3s;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding: clamp(1rem, 1.5vw, 1.25rem);
  gap: clamp(1rem, 1.5vw, 1.25rem);
}

.photo-wrapper:hover .photo-controls-overlay {
  opacity: 1;
}

.control-btn {
  background-color: rgba(255, 255, 255, 0.1);
  color: #ffffff;
  border-radius: 50%;
  width: clamp(2.25rem, 3vw, 2.5rem);
  height: clamp(2.25rem, 3vw, 2.5rem);
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s;
}

.control-btn:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

/* 照片信息栏 */
.photo-info {
  padding: clamp(0.5rem, 0.75vw, 0.625rem) clamp(1rem, 1.5vw, 1.25rem);
  background-color: rgba(30, 41, 59, 0.8);
  display: flex;
  justify-content: space-between;
  font-size: clamp(0.8125rem, 0.9vw, 0.875rem);
  color: #94a3b8;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item .el-icon {
  margin-right: clamp(0.25rem, 0.375vw, 0.375rem);
}

/* 响应式适配 */
@media (max-width: 1600px) {
  .grid-layout-6 {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 1440px) {
  .grid-layout-6 {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 1024px) {
  .grid-layout-4,
  .grid-layout-6 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: clamp(1rem, 1.5vw, 1.25rem);
  }

  .header-actions {
    gap: clamp(0.375rem, 0.5vw, 0.5rem);
  }

  .back-home-btn span {
    display: none;
  }

  .layout-switcher {
    justify-content: center;
  }

  .photo-title {
    max-width: 60%;
  }
}

@media (max-width: 576px) {
  .header-content {
    height: clamp(3rem, 4vw, 3.5rem);
  }

  .page-title {
    font-size: clamp(0.875rem, 1.25vw, 1rem);
  }

  .photo-title {
    max-width: 50%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@media (max-width: 480px) {
  .photo-title {
    font-size: clamp(0.875rem, 1vw, 1rem);
  }

  .photo-status {
    font-size: clamp(0.75rem, 0.875vw, 0.875rem);
    gap: clamp(0.75rem, 1vw, 1rem);
  }

  .photo-info {
    flex-direction: column;
    gap: clamp(0.375rem, 0.5vw, 0.5rem);
  }
}

@media (min-width: 1920px) {
  .photo-container {
    max-width: 100%;
    padding: 0 clamp(2rem, 3vw, 3rem) clamp(4rem, 5vw, 5rem);
  }

  .grid-layout-6 {
    grid-template-columns: repeat(4, 1fr);
  }
}
</style>
